package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.dao.base.TakeTimeDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.domain.utils.BaseAction;
import cn.itcast.bos.service.base.CourierService;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class CourierAction extends BaseAction<Courier> {

	@Autowired
	private CourierService courierService;

	/***
	 * 保存快递员信息
	 * 
	 * @return
	 */
	@Action(value = "courier_save", results = {
			@Result(name = "success", location = "/pages/base/courier.html", type = "redirect") })
	public String saveCourier() {
		courierService.save(model);

		return SUCCESS;
	}

	/***
	 * 查询所有快递员信息
	 * 
	 * @return
	 */
	@Action(value = "findAllCourier", results = { @Result(name = "success", type = "json") })
	public String findAll() {
		// 封装Pageable对象
		Pageable pageable = new PageRequest(page - 1, rows);
		// 封装条件查询对象
		Specification<Courier> spec = new Specification<Courier>() {

			/***
			 * root:用于获得属性字段 CriteriaQuery:可以用于简单条件查询 CriteriaBuilder:用于构造复杂条件查询
			 */
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> pred = new ArrayList<Predicate>();

				// 判断条件是否为空
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					pred.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("Company").as(String.class), "%" + model.getCompany() + "%");
					pred.add(p2);
				}
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p3 = cb.equal(root.get("type").as(String.class), model.getType());
					pred.add(p3);
				}
				// 多表查询
				Join<Standard, Courier> standard = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && StringUtils.isNotBlank(model.getStandard().getName())) {
					Predicate p4 = cb.like(standard.get("name").as(String.class),
							"%" + model.getStandard().getName() + "%");
					pred.add(p4);
				}

				return cb.and(pred.toArray(new Predicate[0]));
			}
		};
		// 传递参数
		Page<Courier> cour = courierService.findAll(spec, pageable);
		pushPageDataToValueStack(cour);

		return SUCCESS;
	}

	/***
	 * 快递员作废
	 */
	// 为参数提供属性驱动
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "courier_delBatch", results = {
			@Result(name = "success", type = "redirect", location = "/pages/base/courier.html") })
	public String delCourier() {
		// 1.获取参数
		System.out.println(ids);
		String[] split = ids.split(",");
		// 2.调用业务层方法
		courierService.delCourier(split);
		return SUCCESS;
	}

	/***
	 * 快递员还原
	 */
	@Action(value = "courier_restoreBatch", results = {
			@Result(name = "success", type = "redirect", location = "/pages/base/courier.html") })
	public String restoreBatch() {
		// 1.获取参数
		System.out.println(ids);
		String[] split = ids.split(",");
		// 2.调用业务层方法
		courierService.restoreBatch(split);
		return SUCCESS;

	}

	/***
	 * 查询快递员
	 */
	@Action(value = "findAllCouriers", results = { @Result(name = "success", type = "json") })
	public String findNoFixedAreaCourier() {

		List<Courier> couriers = courierService.findNoFixedAreaCourier();

		ServletActionContext.getContext().getValueStack().push(couriers);

		return SUCCESS;

	}

	/***
	 * 定区关联快递员 快递员关联定区
	 */
	@Autowired
	private TakeTimeDao tikedao;

	private Integer takeTimeId;

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value = "fixedArea_associationCourierToFixedArea", results = {
			@Result(name = "success", type = "redirect", location = "pages/base/fixed_area.html") })
	public String assactionCourier() {
		// 查询出快递员
		Courier co = courierService.findById(model.getId());
		// 查询出收派时间
		TakeTime takeTime = tikedao.findById(takeTimeId);
		co.setTakeTime(takeTime);

		return SUCCESS;
	}

}
